package com.city.ajax.command;

import com.city.config.BaiduMap;
import com.city.domain.city.CityTown;
import com.city.domain.command.CtGridScatter;
import com.city.domain.pub.PubUser;
import com.city.domain.report.CtReport;
import com.city.format.CtReportFormat;
import com.city.format.EvaluatesFormat;
import com.city.helper.EvalutesRespontory;
import com.city.helper.FormatHelper;

import com.city.service.city.impl.CityTownServiceImpl;
import com.city.service.command.CtGridScatterService;
import com.city.service.command.impl.CtGridScatterServiceImpl;
import com.city.service.pub.impl.PubUserServiceImpl;
import com.city.service.report.impl.ReportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.*;

/**
 * @Author 何平
 * @Description //TODO CommanAjax 综合指挥数据请求
 * @Date 14:39 2018/8/2
 **/

@Controller
@RequestMapping("/ajax/commandajax")
public class CommandAjax {

    @Autowired
    private ReportServiceImpl reportService;

    @Autowired
    private CityTownServiceImpl cityTownService;
    @Autowired
    private EvalutesRespontory evalutesRespontory;
    @Autowired
    private FormatHelper formatHelper;

    @Autowired
    private CtGridScatterServiceImpl ctGridScatterService;



    /**
     * @Author 何平
     * @Description //TODO findAll 地图所有案件信息
     * @Date 14:51 2018/8/2
     * @Param []
     * @return java.util.List<com.city.domain.city.CityEvent>
     **/
    @RequestMapping("/findAll")
    @ResponseBody
    public List<CtReport> findAll(){
        return reportService.findAllReportByScheduleLimit(1,7);
    }

    /*
     * @Author 何平
     * @Description //TODO last_msg 地图最新消息
     * @Date 9:23 2018/8/5
     * @Param []
     * @return java.util.List<com.city.domain.report.CtReport>
     **/
    @RequestMapping("/last_msg")
    @ResponseBody
    public List<CtReportFormat> last_msg(){
        List<CtReport> list = reportService.findAllReportByScheduleLimit(1,7);
        return formatHelper.ctReportFormat(list);
    }


    /**
     * @Author 何平
     * @Description //TODO grid_all 所有网格员
     * @Date 14:18 2018/8/3
     * @Param []
     * @return java.util.List<com.city.domain.pub.PubUser>
     **/
    @RequestMapping("/grid_all")
    @ResponseBody
    public List<CtGridScatter> grid_all(){return ctGridScatterService.findAll();}


    @RequestMapping("/scatter_detail")
    @ResponseBody
    public Map<String,Object> scatter_detail( @Param("id") Integer id){

        Map map = new HashMap();
        //详情
        List<CtReport> ctReports = reportService.findAllReportByReportId(id);
        if(ctReports == null){
            return null;
        }

        map.put("code",0);
        map.put("msg","");
        map.put("count",ctReports.size());
        map.put("data",formatHelper.ctReportFormat(ctReports));
        return map;
    }


    /*
     * @Author 何平
     * @Description //TODO evaluates 返回所有评价信息
     * @Date 19:26 2018/8/3
     * @Param []
     * @return java.util.List<com.city.domain.command.HpEvaluate>
     **/
    @RequestMapping("/evaluates")
    @ResponseBody
    public Map<String,Object> evaluates(@Param("key%5Bid%5D")Integer id,@Param("page") Integer page,@Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<EvaluatesFormat> list = null;
        int count=10;

        if(id != null){
            //搜索

            list = evalutesRespontory.findAllById(id);

        }else if(page != null){
            //分页
            list = evalutesRespontory.findAllByLimit((page-1)*limit,limit);
            count = reportService.findAllReport().size();

            map.put("code",0);
            map.put("msg","");
            map.put("count",count);
            map.put("data",list);
        }

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);




        return map;
    }

    /**
     * @Author 何平
     * @Description //TODO 数据综合展示，表格数据
     * @Date 16:50 2018/8/12
     * @Param []
     * @return java.util.List<com.city.format.CtReportFormat>
     **/
    @RequestMapping("/data_show_table")
    @ResponseBody
    public Map<String,Object> data_show_table(){
        Map<String,Object> map = new HashMap<>();
        List<CtReportFormat> list =  formatHelper.ctReportFormat(reportService.findAllReportByScheduleLimit(0,7));
        map.put("code",0);
        map.put("msg","");
        map.put("count",10);
        map.put("data",list);
        return map;
    }

    /**
     * @Author 何平
     * @Description //TODO 数据综合展示，所有数据信息
     * @Date 10:11 2018/8/14
     * @Param []
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping("/data_show")
    @ResponseBody
    public Map<String,Object> data_show(){
        Map<String,Object> map = new HashMap<>();
        List<Integer> years = reportService.findAllYear();
        List<CityTown> towns = cityTownService.findName("");


        Map<String,List<String>> months = new HashMap<>();
        Map<String,Map<String,Integer>> townYear = new HashMap<>();
        map.put("towns",towns);
        map.put("year",years);
        for (int year:years) {
           months.put(""+year,reportService.findAllByYear(year));
            Map<String,Integer> townNum = new HashMap<>();
            for (CityTown city:towns) {
                townNum.put(city.getTownName(),reportService.findAllByYearAndTownId(year,city.getTownId()));
            }
            townYear.put(""+year,townNum);
        }
        map.put("month",months);
        map.put("townYear",townYear);


        map.put("year_report_default",reportService.findAllByScheduleAndYear(0,2018));
        map.put("year_report_accept",reportService.findAllByScheduleAndYear(3,2018));
        map.put("year_report_over",reportService.findAllByScheduleAndYear(6,2018));

        map.put("month_report_default",reportService.findAllByScheduleAndMonth(0,2018,7));
        map.put("month_report_accept",reportService.findAllByScheduleAndMonth(3,2018,7));
        map.put("month_report_over",reportService.findAllByScheduleAndMonth(6,2018,7));

        map.put("day_report_default",reportService.findAllByScheduleAndDay(0));
        map.put("day_report_accept",reportService.findAllByScheduleAndDay(3));
        map.put("day_report_over",reportService.findAllByScheduleAndDay(6));

        return map;
    }

    /**
     * @Author 何平
     * @Description //TODO notice 逆向提醒报告
     * @Date 21:18 2018/9/28
     * @Param [page, limit]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping("/notice")
    @ResponseBody
    public Map<String,Object> notice(@Param("page") Integer page,@Param("limit") Integer limit){
        Map<String,Object> map = new HashMap<>();
        List<CtReport> list = null,data = new ArrayList<>();
        int count=10;


        //分页

        list =  reportService.findAllReport();
        if(list == null){
            map.put("code",0);
            map.put("msg","");
            map.put("count",count);
            map.put("data",list);
            return map;
        }
        for (int i=(page-1)*limit;i<limit*page;i++) {
            data.add(list.get(i));
        }
        count = reportService.findAllReport().size();

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",formatHelper.ctReportFormat(data));

        return map;
    }
}
